const db = require('../config/db');

class WebSocketConnection {
  static addConnection(userId, connectionId, callback) {
    console.log(`Adding WebSocket connection for user ${userId}`);
    db.run(
      'INSERT OR REPLACE INTO websocket_connections (user_id, connection_id) VALUES (?, ?)',
      [userId, connectionId],
      callback
    );
  }

  static getConnection(userId, callback) {
    console.log(`Retrieving WebSocket connection for user ${userId}`);
    db.get(
      'SELECT connection_id FROM websocket_connections WHERE user_id = ?',
      [userId],
      (err, row) => {
        if (err) {
          console.error('Database error:', err);
          return callback(err);
        }
        if (!row) {
          console.log(`No connection found for user ${userId}`);
          return callback(null, null);
        }
        callback(null, row.connection_id);
      }
    );
  }

  static removeConnection(userId, callback) {
    console.log(`Removing WebSocket connection for user ${userId}`);
    db.run(
      'DELETE FROM websocket_connections WHERE user_id = ?',
      [userId],
      callback
    );
  }
}

module.exports = WebSocketConnection; 